/**
****************************************************************************************
 * @Author: luoweiyong fortwater969@163.com
 * @Version: 
 * @Date: 2024-06-29 22:04:06
 * @LastEditors: luoweiyong fortwater969@163.com
 * @LastEditTime: 2024-06-29 22:04:06
 * @FilePath: \code\Bsp\user\led.c
 * @Description: 
 * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
****************************************************************************************
*/
#include "stm8s.h"
#include "ws2812b.h"
#include "user_apps.h"
#include "user_led.h"

/**
  * @brief  Configure ws2812b 设置第几个灯亮起
  * @param  color 颜色
  * @retval None
  */
void ws2812b_rgb_light(u8 num, u32 color)
{
    if(num > WS2812_LED_NUM)
        return;

    for(u8 i = 0; i < WS2812_LED_NUM; i++)
    {
        if(i == num)
        {
            ws2812b_write(color);
        }
        else
        {
            ws2812b_write(0);
        }
    }
}

/**
  * @brief  Configure ws2812b 从第1颗灯到第5颗灯依次亮
  * @param  color 颜色
  * @retval None
  */
void ws2812b_rgb_effect(u32 color, u16 interval_time)
{
    for(u8 num = 0; num < WS2812_LED_NUM; num++)
    {
        ws2812b_rgb_light(num, WS2812B_COLOR_YELLOW);
        DelayMs(interval_time);
    }
}

/**
  * @brief  Configure ws2812b 流水灯效果
  * @param  color 颜色
  * @param  interval_time 间隔时间
  * @retval None
  */
void ws2812b_rgb_water_lamp(u32 color, u16 interval_time)
{
    for(u8 i = 0; i < WS2812_LED_NUM; i++)
    {
        for(u8 j = 0; j < WS2812_LED_NUM; j++)
        {
            if(j == i)
            {
                ws2812b_write(color);
            }
            else
            {
                ws2812b_write(0x00);
            }
        }
        DelayMs(interval_time);
    }
}

void led_fill_data(u8 cnt, u8 green, u8 red, u8 blue)
{
    switch(cnt)
    {
        case 0:
            rgb_write_data(green, red, blue);
            break;

        case 1:
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            break;

        case 2:
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            break;

        case 3:
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            break;
        
        case 4:
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            rgb_write_data(green, red, blue);
            break;

        default:
            break;
    }
}

/**
  * @brief  Configure ws2812b 左转效果
  * @param  color 颜色
  * @param  interval_time 间隔时间
  * @retval None
  */
void ws2812b_rgb_water_fill_lamp(u8 green, u8 red, u8 blue, u16 interval_time)
{
    for(u8 i = 0; i < 5; i++)
    {
        led_fill_data(i, green, red, blue);
        DelayMs(interval_time);
    }
}

void ws2812b_all_led_off(void)
{
    rgb_write_data(0, 0, 0);
    rgb_write_data(0, 0, 0);
    rgb_write_data(0, 0, 0);
    rgb_write_data(0, 0, 0);
    rgb_write_data(0, 0, 0);
}
